const token=localStorage.getItem("token")

const insertBtn = document.getElementById("insertBtn")
const selectTeacherBtn = document.getElementById("selectTeacherBtn")
const selectCourseBtn = document.getElementById("selectCourseBtn")

const classNameInput=document.getElementById("className")
const autoCkb =document.getElementById("auto");
const dayOfWeekSelect=document.getElementById("dayOfWeek");
const indexClassNum=document.getElementById("indexClass");
const activeCkb =document.getElementById("active");
const minNum=document.getElementById("min");
const maxNum=document.getElementById("max");

const selectCourseTable=document.getElementById("selectCourseTable");
const selectTeacherTable=document.getElementById("selectTeacherTable");

const selectCourseLabel=document.getElementById("selectCourseLabel");
const selectTeacherLabel = document.getElementById("selectTeacherLabel");

var selectedCourseId=-1
var selectedTeacherId =-1

// 按下按钮就会展示或者刷新一次课程列表
selectCourseBtn.addEventListener("click",  async function(){
    selectCourseTable.style.display="table"
    const data=await requestCourse()
    console.log(data.courses)
    displayCourseTable(data.courses)
})

selectTeacherBtn.addEventListener("click", async function(){
    selectTeacherTable.style.display="table"
    const data=await requestTeacher()
    displayTeacherTable(data)
})

insertBtn.addEventListener("click", async () => {
    const url=`${config.server}`+"/api/admin/insert/class"
    const data={
        class_name:classNameInput.value,
        course_id:selectedCourseId,
        teacher_id:selectedTeacherId,
        auto:autoCkb.checked,
        day_of_week:parseInt(dayOfWeekSelect.value),
        index:parseInt(indexClassNum.value),
        min:parseInt(minNum.value),
        max:parseInt(maxNum.value),
        already_active:activeCkb.checked,
        token:token
    }
    console.log(data)
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        console.log("请求成功",getdata)
        location.reload()
    }else{
        console.log("请求失败",getdata)
    }
})

function displayCourseTable(courses){
    tbody=selectCourseTable.querySelector("tbody")
    tbody.innerHTML = "";
    courses.forEach(function (oneCourse) {
        const newRow = document.createElement("tr");
        //遍历对象属性并添加到表格行

        const newData1 = document.createElement("td");
        newData1.textContent = oneCourse.CourseName;
        newRow.appendChild(newData1);

        const newData2 = document.createElement("td");
        newData2.textContent = oneCourse.Introduction;
        newRow.appendChild(newData2);

        const newData3 = document.createElement("td");
        newData3.textContent = oneCourse.CourseSubject;
        newRow.appendChild(newData3);

        const newData4 = document.createElement("td");
        let type=""
        if (oneCourse.IsVipType){
            type="私教"
        }
        if (oneCourse.IsTeamType){
            type="小班"
        }
        if (oneCourse.IsGroupType){
            type="团课"
        }
        newData4.textContent = type;
        newRow.appendChild(newData4);

        //创建复选框单元
        const buttonCell = document.createElement("td");
        const button = document.createElement("button");
        button.innerText="确认"

        // 复选框点击事件
        button.addEventListener("click", function () {
            selectedCourseId=oneCourse.CourseId
            console.log("已经选择了",selectedCourseId)
            selectCourseTable.style.display="none";
            selectCourseLabel.innerText="已经选择了课程: "+oneCourse.CourseName+"再次点击选择可以重新选择"
        });
        buttonCell.appendChild(button);
        newRow.appendChild(buttonCell);
        // 将新行添加到表格
        tbody.appendChild(newRow);
    });
}

function displayTeacherTable(data){
    tbody=selectTeacherTable.querySelector("tbody")
    tbody.innerHTML ="";
    data.forEach(function(teacher){
        const newRow=document.createElement("tr");

        const newData1=document.createElement("td");
        newData1.textContent=teacher.UserInfo.Name
        newRow.appendChild(newData1)
        const buttonCell = document.createElement("td");
        const button = document.createElement("button");
        button.innerText="确认"

        // 复选框点击事件
        button.addEventListener("click", function () {
            selectedTeacherId=teacher.TeacherInfo.TeacherID
            console.log("已经选择了",selectedTeacherId)
            selectTeacherTable.style.display="none";
            selectTeacherLabel.innerText="已经选择了老师: "+teacher.TeacherInfo.TeacherID+"id为: "+selectedTeacherId+"再次点击选择可以重新选择"
        });
        buttonCell.appendChild(button);
        newRow.appendChild(buttonCell);
        // 将新行添加到表格
        tbody.appendChild(newRow);
    })
}



async function requestCourse(){
    const url=`${config.server}`+"/api/select/all/course"
    const data={
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        console.log("请求成功",getdata)
        return getdata
    }else{
        console.log("请求失败",getdata)
    }
}
// /api/select/teacher
async function requestTeacher(){
    const url=`${config.server}`+"/api/select/teacher"
    const data={
    }
    const response=await fetch(url,{
        method:"POST",
        headers: { 'Content-Type': 'application/json' },
        body:JSON.stringify(data)
    })
    const getdata=await response.json()
    if(response.ok){
        console.log("请求成功",getdata)
        return getdata
    }else{
        console.log("请求失败",getdata)
    }
}